<template>
  <section class="news">
    <template v-for="(item, index) in newsList">
      <div v-if="index < 4" class="new" :key="item.id">
        <div class="picture" @click="toDetail(item.id)">
          <el-image
            style="width: 288px; height: 145px"
            :src="item.frontCover"
            fit="fit"
          ></el-image>
        </div>
        <div class="content">
          <el-link
            :underline="false"
            type="default"
            class="title"
            @click="toDetail(item.id)"
            >{{ item.title | substr }}</el-link
          >
          <div class="sub">
            <span class="time">{{ item.createdAt }}</span>
            <span class="views">{{ item.visitCount }}人浏览</span>
          </div>
        </div>
      </div>
    </template>
    <div class="more" @click="toNewList">更多新闻</div>
  </section>
</template>

<script>
import { getNewsList } from "@/api/api.js";
export default {
  name: "News",
  data() {
    return {
      newsList: [],
      title:
        "新闻标题内容新闻标题内容新闻标题内容新闻标题内容新闻标题内容新闻标题内容",
    };
  },
  created() {
    this.getNewsList();
  },
  methods: {
    getNewsList() {
      getNewsList().then((res) => {
        console.info("新闻列表", res);
        this.newsList = res.data.list || [];
      });
    },
    toDetail(id) {
      console.info(id);
      this.$router.push({ path: "/NewsDetail", query: { id: id } });
    },
    // 进入新闻列表也查看更多新闻
    toNewList() {
      console.info("更多新闻");
      this.$router.push({ path: "/NewsList" });
    },
  },
  filters: {
    substr: function(value) {
      if (value.length > 29) {
        value = value.substring(0, 28) + "……";
      }
      return value;
    },
  },
};
</script>

<style lang="scss" scoped>
.news {
  height: 270px;
  display: flex;
  justify-content: space-between;
  position: relative;

  .new {
    width: 288px;
    box-shadow: 0px 2px 5px 3px rgba(0, 0, 0, 0.06);

    .picture {
      cursor: pointer;
    }

    .content {
      height: 125px;
      padding: 20px;

      .title {
        height: 50px;
        overflow: hidden;
        margin-bottom: 20px;
        font-size: 16px;
        line-height: 21px;
        color: #303133;
        display: block;
        text-align: left;
      }

      .sub {
        display: flex;
        justify-content: space-between;
        line-height: 18px;
        color: #606266;
      }
    }
  }

  .more {
    position: absolute;
    width: 40px;
    height: 145px;
    right: 0px;
    top: 0px;
    background: rgba(0, 0, 0, 0.5);
    padding: 30px 12px;
    font-size: 16px;
    line-height: 21px;
    color: #ffffff;
    cursor: pointer;
  }
}
</style>
